package com.woniuxy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.entity.Cart;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.Serializable;
import java.util.List;

/**
 * 购物车Mapper接口
 */
public interface CartMapper extends BaseMapper<Cart> {
    /**
     * 查找用户购物车中所有商品
     * @param userId
     * @return
     */
    @Select("select dish.* ,cart.id cid,cart.userId userId,cart.dishId dishid,cart.nums nums,cart.selected,cart.money from gfr_cart cart,gfr_dish dish where cart.dishId=dish.id and cart.userId=#{userId}")
    @Results({
            @Result(column = "cid", property = "id"),
            @Result(column = "dishId", property = "dishId"),
            @Result(column = "dishId", property = "dish.id"),
            @Result(column = "name", property = "dish.dishName"),
            @Result(column = "market_price", property = "dish.marketPrice"),
            @Result(column = "price", property = "dish.price"),
            @Result(column = "shopId", property = "dish.shopId"),
            @Result(column = "dishCategoryId", property = "dish.dishCategoryId"),
            @Result(column = "picture", property = "dish.picture"),
    })
    List<Cart> getByCondition(Integer userId);

    /**
     * 查找购物车中选中的商品
     */
    @Select(" select dish.* ,cart.id cid,cart.userId userId,cart.dishId dishid,cart.nums nums,cart.selected,cart.money from gfr_cart cart,gfr_dish dish where cart.dishId=dish.id and userId=#{userId} and cart.id=#{id}")
    @Results({
            @Result(column = "cid", property = "id"),
            @Result(column = "dishId", property = "dishId"),
            @Result(column = "dishId", property = "dish.id"),
            @Result(column = "name", property = "dish.dishName"),
            @Result(column = "market_price", property = "dish.marketPrice"),
            @Result(column = "price", property = "dish.price"),
            @Result(column = "shopId", property = "dish.shopId"),
            @Result(column = "dishCategoryId", property = "dish.dishCategoryId"),
            @Result(column = "picture", property = "dish.picture"),
    })
    Cart getByDish(@Param("userId") Integer userId,@Param("id") Integer id);


    /**
     * 根据用户ID和商品ID 查看用户购物车中是否有此商品
     * @param userid
     * @param dishid
     * @return
     */
    @Select("select COUNT(*) from gfr_cart where userId=#{userid} and dishId=#{dishid}")
    int sel(@Param("userid")int userid,@Param("dishid")int dishid);

    /**
     * 查找用户指定的购物车数据
     */
    @Select("select*from gfr_cart where userId=#{userid} and dishId=#{dishid}")
    Cart selCart(@Param("userid") int userid, @Param("dishid") int dishid);


    /**
     * 删除购物车数据
     * @param id
     * @param userid
     */
    @Delete("delete from gfr_cart where id=#{id} and userId=#{userid}")
    void del(int id,int userid);
}
